/**
 * Created by buhuayang on 16/7/13.
 */
var ChatRoom = function () {
    var sock = null;
    var connect = function () {
        if (sock == null) {
            sock = new SockJS('/wsstudy/sockjs/ws');
            sock.onopen = function () {
                console.log('open');
                $.ajax({
                    type: "get",
                    url: ChatRoom.getRootPath() + '/online-list.htm',
                    dataType: "html",
                    cache: false,
                    success: function (resp) {
                        $("#online-list").html("");
                        $("#online-list").html(resp);
                    }
                });
            };
            sock.onmessage = function (msgData) {
                console.log('message', msgData.data);
                var msgInfo = JSON.parse(msgData.data);
                if (msgInfo.type == "list-changed") {
                    $.ajax({
                        type: "get",
                        url: ChatRoom.getRootPath() + '/online-list.htm',
                        dataType: "html",
                        cache: false,
                        success: function (resp) {
                            $("#online-list").html("");
                            $("#online-list").html(resp);
                        }
                    });
                } else {
                    var chatViewHtml = "<div class='alert alert-info text-left' role='alert'>" +
                        "<span class='label label-primary'>" + msgInfo.fromUsername + "</span>&nbsp;" + msgInfo.value + "</div>";
                    $("#chat-panel .panel-body").append(chatViewHtml);
                    var lastMsgDiv = $("#chat-panel > .panel-body > div:last");
                    if (lastMsgDiv.length) {
                        $("#chat-panel").scrollTo(lastMsgDiv, 200);
                    }
                }
            };
            sock.onclose = function () {
                console.log('close');
            };
        }
    }
    return {
        init: function () {
            connect();
            $("button").on("click", function () {
                var username = $("#username").val();
                var msgText = $("#msgText").val();
                if (msgText && msgText.length && msgText.length > 0) {
                    var msgInfo = {
                        type: "text-msg",
                        fromUsername: username,
                        value: msgText
                    };
                    var chatViewHtml = "<div class='alert alert-success text-right' role='alert'>" + msgInfo.value +
                        "&nbsp;<span class='label label-primary'>" + msgInfo.fromUsername + "</span></div>";
                    $("#chat-panel .panel-body").append(chatViewHtml);
                    var lastMsgDiv = $("#chat-panel > .panel-body > div:last");
                    if (lastMsgDiv.length) {
                        $("#chat-panel").scrollTo(lastMsgDiv, 200);
                    }
                    sock.send(JSON.stringify(msgInfo));
                }
            });
        },
        getRootPath: function rootPath() {
            var path = window.location.pathname;
            return path.substring(0, path.substr(1).indexOf('/') + 1);
        }
    }
}();
$(function () {
    ChatRoom.init();
});